using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace ImageReview
{
    public partial class Accept : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session[ "ShowBarcodeFlag" ] = false;
            Session["ShowDeleteLink"] = false;
            Session["ConsignmentNumberReadonly"] = true;
            Session["GoodsIDReadonly"] = true;
            Session["CurrentPage"] = CurrentPage.Accept;

            // Determine the user name, less domain
            string username = User.Identity.Name;
            username = username.Substring( username.LastIndexOf( '\\' ) + 1 );

            // Maybe determine the default folder for this user.
            if( Session[ "FolderToLoad" ] == null || String.IsNullOrEmpty( Session[ "FolderToLoad" ].ToString( ) ) )
            {
                // Retrieve configuration xml file.
                TPDSConfig.TPDSConfig ConfigFile = TPDSWEBConfig.Load( Server.MapPath( @"~/" ) );
                if( ConfigFile == null )
                {
                    Response.Write( "<script type=\"text/javascript\">alert('Unable to load the configuration file (config.xml).');</script>" );
                    return;
                }

                // Determine the folder for this user.
                Session[ "FolderToLoad" ] = Server.MapPath( "~/" + Path.Combine( ConfigFile.ImageMonitorRootFolder, username ) );
            }

            // Cannot go further without a folder in session state
            if( Session[ "FolderToLoad" ] == null || String.IsNullOrEmpty( Session[ "FolderToLoad" ].ToString( ) ) )
                return;

            if( !IsPostBack )
            {
                Label lblUsername = (Label)this.FindControl( "lblUsername" );
                lblUsername.Text = "User: ";
                Label lblUsername2 = (Label)this.FindControl( "lblUsername2" );
                lblUsername2.Text = username;

                FolderName.Text = Session[ "FolderToLoad" ].ToString( );
            }

            // Populate interface.
            PopulatePreviewTable( );
        }

        private void PopulatePreviewTable( )
        {
            if( !Directory.Exists( Session[ "FolderToLoad" ].ToString( ) ) )
            {
                Response.Write( "<script type=\"text/javascript\">alert('Users image folder does not exist.');</script>" );
                return;
            }

            PreviewTable previewTable = (PreviewTable)this.FindControl( "previewTable1" );
            previewTable.Imagesets = TPDSControl.ImageSet.LoadAll( Session[ "FolderToLoad" ].ToString( ) );
        }
    }
}
